﻿//用于商家模块标识翻译添加、修改、删除、查询
var $dataAddTemplate = null;
var $dataListTemplate = null;
var $tbodyAdd = null;
var $tbody = null;
var ajaxUrl = "TranslateHandler.ashx";

//DOM樹加載完成
$(function () {
    $dataAddTemplate = $("#dataAddTemplate");
    $dataListTemplate = $("#dataListTemplate");
    $tbodyAdd = $("#tbodyAdd");
    $tbody = $("#tbody");

    getTranslateModuleDDL();//获取翻译模块下拉框
    //getFlagTranslateList();
});

var $ddlModule = null;
var moduleListData = null;

//獲取所有翻譯模块数据
function getTranslateModuleDDL()
{
    $ddlModule = $("#ddlModule");
    $ddlModule.on('change',function() {
        getFlagTranslateList();
    })
    var data = {
        "Operate": "GetTranslateModuleDDL",
        "SiteId": siteId
    };

    $.ajaxSetup({ async: false });
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            moduleListData = ajaxMsg.data;
            moduleListData = ajaxMsg.data;
            var htmlOptions = $.templates($("#moduleDDLTemplate").text()).render(moduleListData);//填充模块下拉框
            $ddlModule.append(htmlOptions);
            getFlagTranslateList();
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
    $.ajaxSetup({ async: true });
}

var isFirstLoad=true;//是否是首次加載數據
var sortField = null;
var isDesc = null;

//獲取排序字段和方法
function getSortInfo()
{
    sortField = "FlagId";
    isDesc = false;
    return;
}

//根據模塊Id獲取模塊名稱
//moduleId 模塊Id
function getModuleNoByModuleId(moduleId)
{
    for (var i = 0; i < moduleListData.length; i++)
    {
        if (moduleListData[i].ModuleID == moduleId)
        {
            return moduleListData[i].ModuleNo;
        }
    }
    return "";
}

var columnCount = 0;

//获取標識翻譯列表
function getFlagTranslateList() {
    var strModuleId = $("#ddlModule").val();
    if (isNaN(strModuleId)) {
        alert(txt_Error);
        return;
    }

    var keyword=$("#txtKeyword").val();
    getSortInfo();//獲取排序信息

    var keyword = $("#txtKeyword").val();
    var data = {
        "Operate": "GetFlagTranslateList",
        "SiteId": siteId,
        "ModuleId": strModuleId,
        "SortField": sortField,
        "IsDesc": isDesc,
        "Keyword": keyword
    };

    //msgBox.showMsgWait(txt_Loading); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (ajaxMsg.data.length > 0) {//有数据
                if (columnCount == 0){
                    for(column in ajaxMsg.data[0])
                    {
                        if(column.indexOf("Result_")!=-1)
                        {
                            columnCount++;
                        }
                    }
                }
                var html = $.templates($dataListTemplate.text()).render(ajaxMsg.data);
                $tbody.html(html);
                $tbody.children().each(function (i) {
                    var $tr = $(this);
                    var strFlagId = $tr.attr("id").replace("tr_", "");
                    var $tdModule = $("#tdModule_" + strFlagId, $tr);
                    var strModuleId = $tdModule.html();
                    var moduleName = getModuleNoByModuleId(strModuleId)
                    $tdModule.html(moduleName);
                });
            }
            else {//无数据
                $tbody.children().remove();
                $tbody.html("<tr><td colspan=" + (columnCount + 3) + ">" + txt_NoData + "</td></tr>");
            }

            msgBox.hidBox(); //隐藏消息面板
            return;//以下为分页代码，暂时没用

            var colspan=$tbody.prev().children().eq(0).children().length;
            if ($tbody.children().length > 0) {//如果已有数据，先移去
                $tbody.children().remove();
            }

            if(ajaxMsg.data.length>0)
            {
                $dataListTemplate.tmpl(ajaxMsg).appendTo($tbody); //添加数据

                $('#pagination-demo').twbsPagination({//分頁
                    totalPages: ajaxMsg.totalPages,
                    visiblePages: 10,
                    onPageClick: function (event, page) {
                        if(!isFirstLoad)
                        {                                    
                            getFiledList(page);
                        }
                        isFirstLoad=false;
                    },
                    first: txt_FirstPage,
                    prev: txt_PreviousPage,
                    next: txt_NextPage,
                    last: txt_LastPage
                });
            }
            else{
                $("<tr><td colspan='" + colspan + "'>" + txt_NoData + "</td></tr>").appendTo($tbody);
            }
            msgBox.hidBox(); //隐藏消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var flag = null;
var resultHK = null;
var resultCN = null;
var resultEN = null;

//修改標識翻譯
//btnSave 當前點擊的保存按鈕
function saveFlagTranslate(btnEdit)
{
    var $btnEdit = $(btnEdit);
    var strFlagId = $btnEdit.attr("id").replace("btnEdit_", "");
    if (isNaN(strFlagId)) {
        alert(txt_Error);
        return;
    }

    var flagId = parseInt(strFlagId);
    if (flagId <= 0) {
        alert(txt_Error);
        return;
    }

    var strModuleId = $btnEdit.attr("moduleid");
    if (isNaN(strModuleId)) {
        alert(txt_Error);
        return;
    }

    var moduleId = parseInt(strModuleId);
    if (moduleId < 0) {
        alert(txt_Error);
        return;
    }

    var data = {
        "Operate": "SaveFlagTranslate",
        "SiteId": siteId,
        "ModuleId": moduleId,
        "FlagId": flagId
    };

    var $tr = $("#tr_" + flagId, $tbody);
    var $txtResults = $("[type='text']", $tr);
    var strLangId = null;
    var langId = 0;
    var strResultId = null;
    var resultId = 0;
    $txtResults.each(function (i) {
        var $txtResult = $(this);
        strLangId = $txtResult.attr("id").replace("txtResult_", "");
        if (isNaN(strLangId))
        {
            alert(txt_Error);
            return;
        }

        langId = parseInt(strLangId);
        if (langId <= 0)
        {
            alert(txt_Error);
            return;
        }

        strResultId = $txtResult.attr("resultid");
        if (isNaN(strResultId))
        {
            alert(txt_Error);
            return;
        }

        resultId = parseInt(strResultId);
        if (resultId < 0) {
            alert(txt_Error);
            return;
        }

        txtResultId = $txtResult.attr("id");
        result = $txtResult.val();
        data[strResultId]=
        data["Result_" + strLangId] = result;
        data["ResultId_" + strLangId] = resultId;
    });

    msgBox.showMsgWait(txt_TryingToModify); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            for (var property in ajaxMsg.data[0])
            {
                $("#txtResult_" + property, $tr).attr("resultid", ajaxMsg.data[0][property]);
            }
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}